<?php

namespace app\bis\controller;

use think\Request;

class Deal extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //显示团购信息
        $bisId = $this->getLoginUser()->bis_id;
        $deal = model('Deal')->getDeal($bisId);
        return $this->fetch('', ['deal' => $deal]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $bisId = $this->getLoginUser()->bis_id;
        if (\request()->isGet()) {
            //获取城市信息
            $citys = model('city')->getNormalCitysByParentId();
            //获取分类信息
            $categorys = model('category')->getNormalFirstCategory();
            //显示所有门店信息
            $location = model('BisLocation')->getLocationName($bisId);
            return $this->fetch('', ['citys' => $citys, 'categorys' => $categorys, 'location' => $location]);
        } else {
            $data = input('post.');
            $validate = validate('deal');
            $start_time = strtotime($data['start_time']);
            $end_time = strtotime($data['end_time']);
            if ($start_time > $end_time) {
                $this->error('开始时间应该小于结束时间');
            }
            $coupons_begin_time = strtotime($data['coupons_begin_time']);
            $coupons_end_time = strtotime($data['coupons_end_time']);
            if ($coupons_begin_time > $coupons_end_time) {
                $this->error('开始时间应该小于结束时间');
            }
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            //数据入库
            //获取经纬度
            $location = model('BisLocation')->get($data['location_ids'][0]);
            $deal = [
                'name' => $data['name'],
                'category_id' => $data['category_id'],
                'se_category_id' => empty($data['se_category_id']) ? '' : implode(',', $data['se_category_id']),
                'bis_id' => $bisId,
                'location_ids' => empty($data['location_ids']) ? '' : implode(',', $data['location_ids']),
                'city_id' => $data['city_id'],
                'image' => $data['image'],
                'start_time' => $data['start_time'],
                'end_time' => $data['end_time'],
                'total_count' => $data['total_count'],
                'origin_price' => $data['origin_price'],
                'current_price' => $data['current_price'],
                'coupons_begin_time' => $data['coupons_begin_time'],
                'coupons_end_time' => $data['coupons_end_time'],
                'description' => empty($data['description']) ? '' : $data['description'],
                'notes' => empty($data['notes']) ? '' : $data['notes'],
                'xpoint' => $location['xpoint'],
                'ypoint' => $location['ypoint']
            ];
            $res = model('Deal')->add($deal);
            if ($res) {
                $this->success('添加成功', url('deal/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
